"use strict";
const index = require("../utils/index.js");
const form = require("../UtFormGroup/form.js");
const index_module = require("./index.module.js");
const vue = require("vue");
const help = require("./help.js");
const formatDataUtil = require("../node_modules/unisoft-web-util/src/util/formatDataUtil.js");
const index$1 = require("../node_modules/element-plus/es/components/table/index.js");
const index$2 = require("../node_modules/element-plus/es/components/pagination/index.js");
const index$3 = require("../node_modules/element-plus/es/components/form/index.js");
const UtTable = vue.defineComponent({
  props: {
    formOption: {
      type: Object
    },
    formButton: {
      type: Object
    },
    tableTitle: {
      type: Object
    },
    table: {
      type: Object
    },
    page: {
      type: Object
    }
  },
  setup(props, context) {
    const { emit, expose, slots } = context;
    const formData = index.initFormModel(props.formOption);
    const form$1 = vue.ref(null);
    const state = vue.reactive({
      tableOption: null,
      page: props.page || {
        pageSize: 10,
        total: 0,
        currentPage: 1
      },
      tableSelect: [],
      tableData: []
    });
    const queryTableData = () => {
      queryTable(1);
    };
    const queryTable = (page) => {
      form$1.value.validate((valid) => {
        if (valid) {
          state.page.currentPage = page || 1;
          emit("queryTable", formData, state.page, ({ total, list }) => {
            state.tableData = formatDataUtil.formatArrayFromSet(list, props.table.list);
            state.page.total = total;
            state.tableSelect = [];
          });
        } else {
          console.log("error submit!");
          return false;
        }
      });
    };
    const currentChange = (value) => {
      queryTable(value);
    };
    const onSelectChange = (val) => {
      state.tableSelect = val;
    };
    const reset = () => {
      form$1.value.reset();
    };
    const getPage = () => {
      return state.page;
    };
    const setPage = (page) => {
      for (const key in page) {
        state.page[key] = page[key];
      }
    };
    const freshTable = () => {
      queryTable(state.page.currentPage);
    };
    const method = {
      queryTableData,
      freshTable,
      reset,
      getPage,
      setPage
    };
    const getClickProp = (row) => {
      return {
        formData,
        select: state.tableSelect,
        tableData: state.tableData,
        row,
        context,
        method
      };
    };
    const tableOption = help.formatTableOption(props.table, context, getClickProp);
    state.tableOption = tableOption;
    const getFormButton = (buttonSet) => {
      const margin = props.formOption ? props.formOption.itemMargin || index.DefaultItemMargin : index.DefaultItemMargin;
      return /* @__PURE__ */ React.createElement(index$3.ElFormItem, {
        style: {
          margin
        }
      }, buttonSet.queryButton && help.getElButtonItem(buttonSet.queryButton, getClickProp(), "\u641C\u7D22", queryTableData), buttonSet.resetButton && help.getElButtonItem(buttonSet.resetButton, getClickProp(), "\u91CD\u7F6E", reset), buttonSet.list && buttonSet.list.map((item) => {
        if (!item.hidden) {
          return help.getSlotView(item, getClickProp(), slots, () => {
            return help.getElButtonItem(item, getClickProp());
          });
        } else {
          return null;
        }
      }));
    };
    const getTableTitle = (titleSet) => {
      const iconStyle = Object.assign({ marginRight: "10px" }, titleSet.iconStyle);
      return /* @__PURE__ */ React.createElement("div", {
        className: index_module.default.tableTitle,
        style: titleSet.titleStyle
      }, /* @__PURE__ */ React.createElement("div", null, titleSet.icon && /* @__PURE__ */ React.createElement("i", {
        className: titleSet.icon,
        style: iconStyle
      }), /* @__PURE__ */ React.createElement("span", {
        style: titleSet.labelStyle
      }, titleSet.label)), /* @__PURE__ */ React.createElement("div", null, titleSet.buttonList && titleSet.buttonList.map((item) => {
        if (!item.hidden) {
          return help.getSlotView(item, getClickProp(), slots, () => {
            return help.getElButtonItem(item, getClickProp());
          });
        } else {
          return null;
        }
      })));
    };
    expose(method);
    const tableStyle = Object.assign(
      {
        width: "100%",
        height: "400px",
        paddingLeft: "10px",
        paddingRight: "10px"
      },
      props.table.style
    );
    return () => {
      return /* @__PURE__ */ React.createElement("div", {
        className: index_module.default.root
      }, props.formOption && /* @__PURE__ */ React.createElement("div", {
        className: index_module.default.formGroup
      }, /* @__PURE__ */ React.createElement(form, {
        ref: form$1,
        option: props.formOption,
        model: formData
      }, help.getSlotView(props.formButton, getClickProp(), slots, () => {
        return getFormButton(props.formButton);
      }))), /* @__PURE__ */ React.createElement("div", {
        className: index_module.default.tableLay
      }, props.tableTitle && help.getSlotView(props.tableTitle, getClickProp(), slots, () => {
        return getTableTitle(props.tableTitle);
      }), /* @__PURE__ */ React.createElement(index$1.ElTable, {
        data: state.tableData,
        stripe: true,
        style: tableStyle,
        ...props.table.props,
        onSelectionChange: onSelectChange
      }, state.tableOption && state.tableOption.map((item) => {
        if (item && !item.hidden) {
          return /* @__PURE__ */ React.createElement(index$1.ElTableColumn, {
            ...item.props
          }, item.slots);
        } else {
          return null;
        }
      })), /* @__PURE__ */ React.createElement(index$2.ElPagination, {
        layout: "prev, pager, next",
        ...state.page,
        "hide-on-single-page": true,
        onCurrentChange: currentChange
      })));
    };
  }
});
module.exports = UtTable;
